import java.util.ArrayList;

/*
 * Created by Jesse Phillips
 * Created on 12-Jan-06
 */

public class Rectangle extends TwoDShape {
	protected double length, width;
	
	public Rectangle(double length, double width) {
		super("Rectangle");
		this.length = length;
		this.width = width;
	}

	public Rectangle(String name, double length, double width) {
		super(name);
		this.length = length;
		this.width = width;
	}

	@Override
	public double area() {
		return length*width;
	}

	@Override
	public double perimeter() {
		return 2*length+2*width;
	}
	
	
	public static String validate(double a, double b, double c, double d) {
		if (!TwoDShape.validate(a))
			return "First side is too small";
		else if (!TwoDShape.validate(b))
			return "Second side is too small";
		else if (!TwoDShape.validate(c))
			return "Third side is too small";
		else if (!TwoDShape.validate(d))
			return "forth side is too small";
		else
			if(a != c)
				return "Opposite sides must be the same";
			else if(b != d)
				return "Opposite sides must be the same";
			else
				return null;
	}
	
	public static Rectangle create(double a, double b, double c, double d) throws ShapeException {
		String error = validate(a,b,c,d);
		if(error == null) 
			return new Rectangle(a, b);
		else {
			ArrayList<Double> foo = new ArrayList<Double>();
			foo.add(a);
			foo.add(b);
			foo.add(c);
			foo.add(d);
			throw(new ShapeException("Rectangle", error, foo));
		}
	}
	
	public String toString() {
		String result = super.toString();
		result += "\nlength: "+length+" width: "+width;
		
		return result;
	}

}
